﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsumeSys.Model;
using ConsumeSys.Orm;

namespace ConsumeSys.BLL
{
    /// <summary>
    /// 消费BLL
    /// </summary>
    public class ConsumeBLL
    {
        /// <summary>
        /// 数据访问层
        /// </summary>
        private ConsumeSys.DAL.ConsumeDAL dal_ConsumeDAL = new DAL.ConsumeDAL();

        #region 消费类型

        /// <summary>
        /// 添加消费类型
        /// </summary>
        /// <param name="consumeType">需要添加的实体</param>
        /// <returns>受影响的行数</returns>
        public int AddConsumeType(Model.ConsumeType consumeType)
        {
            return dal_ConsumeDAL.AddConsumeType(consumeType);
        }

        /// <summary>
        /// 返回单个消费类型
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>ConsumeType</returns>
        public Model.ConsumeType GetConsumeTypeModel(int id)
        {
            return dal_ConsumeDAL.GetConsumeTypeModel(id);
        }

        /// <summary>
        /// 返回所有的消费类型
        /// </summary>
        /// <returns>List</returns>
        public List<Model.ConsumeType> GetConsumeTypeList()
        {
            return dal_ConsumeDAL.GetConsumeTypeList();
        }

        /// <summary>
        /// 消费类型分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="typeName">类型名称</param>
        /// <returns>Paging</returns>
        public Paging<Model.ConsumeType> GetConsumeTypePaging(int pageIndex, int pageSize, string typeName)
        {
            return dal_ConsumeDAL.GetConsumeTypePaging(pageIndex, pageSize, typeName);
        }

        /// <summary>
        /// 删除消费类型
        /// </summary>
        /// <param name="ids">主键集合</param>
        /// <returns>受影响的行数</returns>
        public int DeleteConsumeType(string ids)
        {
            return dal_ConsumeDAL.DeleteConsumeType(ids);
        }

        #endregion

        #region 消费

        /// <summary>
        /// 添加消费类型
        /// </summary>
        /// <param name="consume">Consume</param>
        /// <returns>受影响的行数</returns>
        public int AEConsume(Model.Consume consume)
        {
            return dal_ConsumeDAL.AEConsume(consume);
        }

        /// <summary>
        /// 获取分页信息
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">页大小</param>
        /// <param name="c_Name">名称</param>
        /// <param name="c_SettlementStatus">结算状态</param>
        /// <param name="c_UserId">消费人</param>
        /// <param name="c_CTypeId">类型</param>
        /// <returns>Paging</returns>
        public Paging<Model.Consume> GetConsumePaging(int pageIndex, int pageSize, string c_Name, int? c_SettlementStatus, int c_UserId, int c_CTypeId)
        {
            return dal_ConsumeDAL.GetConsumePaging(pageIndex, pageSize, c_Name, c_SettlementStatus, c_UserId, c_CTypeId);
        }

        /// <summary>
        /// 删除消费
        /// </summary>
        /// <param name="ids">主键集合</param>
        /// <returns>受影响的行数</returns>
        public Result DeleteConsume(string ids, User loginUser)
        {
            //检查是否是自己创建的记录
            int modelId = 0;
            if (!Int32.TryParse(ids, out modelId))
            {
                return new Result() { Code = 500, Msg = "参数必须是整数" };
            }
            var model = dal_ConsumeDAL.Get(modelId);
            //var userBll = new UserBLL();
           // int userId = userBll.GetLoginUser().id;
            int userId = loginUser.id;

            if (model.c_CreateUserId != userId)
            {
                return new Result() { Code = 500, Msg = "只能删除自己创建的记录" };
            }
            int count = dal_ConsumeDAL.DeleteConsume(ids);
            if (count > 0)
            {
                return new Result() { Code = 200, Msg = "删除成功" };
            }
            return new Result() { Code = 500, Msg = "删除失败" };
        }

        #endregion

        /// <summary>
        /// 计算未结算的消费记录，同时修改记录状态为已结算
        /// </summary>
        /// <returns></returns>
        public Object JiSuan()
        {
            int jisuanED = (int)ConsumeSys.Enum.ConsumeEnum.SettlementStatus.已结算;
            int weijisuanED = (int)ConsumeSys.Enum.ConsumeEnum.SettlementStatus.未结算;
            return dal_ConsumeDAL.JiSuan(jisuanED, weijisuanED);
        }

        /// <summary>
        /// 查询往期结算记录
        /// </summary>
        /// <returns></returns>
        public Object GetPageData()
        {
            Object dt = dal_ConsumeDAL.GetPageData();
            return dt;
        }




    }
}
